import time
import json
import requests
from index.models import data_area, data_province

# 抓取新冠疫情的数据接口



def get_datas(last_time):
    if int(time.time())-last_time < 80000:
        return last_time
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
    global data
    data = json.loads(requests.get(url=url).json()['data'])
    #print(data)
    #print(data['lastUpdateTime'],"    ")
    #print(int(time.time()),"      ",int(time.mktime(tr)))
    print(data['lastUpdateTime'])
    ti = data['lastUpdateTime']
    tr = time.strptime(data['lastUpdateTime'], "%Y-%m-%d %H:%M:%S")
    if last_time < int(time.mktime(tr)):
        last_time = int(time.mktime(tr))
        if 'areaTree' in data:
            ct = 1
            areaTree = data['areaTree']
            # print(areaTree)
            for zg in areaTree:
                if 'children' in zg:
                    children = zg['children']
                    for c in children:
                        #print(c)
                        province = data_province.objects.filter(name=c['name'])
                        #print(province)
                        if not province:
                            #print(c)
                            fa=data_province(
                                count = ct,
                                name = c['name'],
                                update_time=ti,
                                today_confirm = c['today']['confirm'],
                                total_nowConfirm = c['total']['nowConfirm'],
                                total_confirm = c['total']['confirm'],
                                total_suspect = c['total']['suspect'],
                                total_dead = c['total']['dead'],
                                total_deadRate = c['total']['deadRate'],
                                total_heal = c['total']['heal'],
                                total_healRate = c['total']['healRate'],)
                            fa.save()
                        else:
                            province.update(
                                update_time=ti,
                                today_confirm = c['today']['confirm'],
                                total_nowConfirm = c['total']['nowConfirm'],
                                total_confirm = c['total']['confirm'],
                                total_suspect = c['total']['suspect'],
                                total_dead = c['total']['dead'],
                                total_deadRate = c['total']['deadRate'],
                                total_heal = c['total']['heal'],
                                total_healRate = c['total']['healRate'],)

                        # data_fa.append([count, name, today_confirm, total_confirm, total_nowConfirm,
                        #                 total_suspect, total_dead, total_deadRate, total_heal, total_healRate])
                        if 'children' in c:
                            children = c['children']
                        for d in children:
                            ar = data_area.objects.filter(name=d['name'])
                            if ar.count()==0:
                                ch=data_area(
                                    name = d['name'],
                                    count_id = ct,
                                    update_time=ti,
                                    today_confirm = d['today']['confirm'],
                                    total_nowConfirm = d['total']['nowConfirm'],
                                    total_confirm = d['total']['confirm'],
                                    total_suspect = d['total']['suspect'],
                                    total_dead = d['total']['dead'],
                                    total_deadRate = d['total']['deadRate'],
                                    total_heal = d['total']['heal'],
                                    total_healRate = d['total']['healRate'],)
                                ch.save()
                            else:
                                ar.update(
                                    update_time=ti,
                                    today_confirm = d['today']['confirm'],
                                    total_nowConfirm = d['total']['nowConfirm'],
                                    total_confirm = d['total']['confirm'],
                                    total_suspect = d['total']['suspect'],
                                    total_dead = d['total']['dead'],
                                    total_deadRate = d['total']['deadRate'],
                                    total_heal = d['total']['heal'],
                                    total_healRate = d['total']['healRate'],)


                                # data_ch.append(
                                #     [count, name, today_confirm, total_confirm, total_nowConfirm,
                                #      total_suspect, total_dead, total_deadRate, total_heal, total_healrate])
                                # print([count, name, today_confirm, total_confirm, total_nowConfirm, total_suspect, total_dead,total_deadRate, total_heal])
                            #count = count + 1
                        ct+=1



        # with open('data_province.csv', 'w' , encoding='gb2312') as f:
        #     f.write('count , name , today_confirm , total_confirm , total_nowConfirm , total_suspect , total_dead , total_deadRate , total_heal , total_healRate' + '\n')
        #     for i in range(len(data_fa)):
        #         for j in range(len(data_fa[i])):
        #             f.write(str(data_fa[i][j]) + ',')
        #         f.write('\n')
        #     f.close()
        #
        # with open('data_area.csv','w' , encoding='gb2312') as f:
        #     f.write('count , name , today_confirm , total_confirm , total_nowConfirm , total_suspect , total_dead , total_deadRate , total_heal , total_healRate' + '\n')
        #     for i in range(len(data_ch)):
        #         for j in range(len(data_ch[i])):
        #             f.write(str(data_ch[i][j]) + ',')
        #         f.write('\n')
        #     f.close()




    return last_time
